import 'package:flutter/material.dart';
import 'package:news_flutter/utils/precompile.dart';
/*
* 你编写 UIView 的子类，或使用已经存在的 view 来重载并实现方法，以达到特定的功能。在 Flutter 中，你会组合（composing）多个小的 widgets 来构建一个自定义的 widget（而不是扩展它）。

举个例子，如果你要构建一个 CustomButton ，并在构造器中传入它的 label？那就组合 RaisedButton 和 label，而不是扩展 RaisedButton。
* */
enum MyAppBarStyle {
  //红色背景，白色文字
  RED,
  //白色背景，黑色文字
  WHITE,
}
class _MyAppBarTheme{
  Color backgroundColor;
  Color iconColor;
  Color textColor;
  Brightness brightness;
  final MyAppBarStyle myAppBarStyle;
  _MyAppBarTheme(this.myAppBarStyle){
    if (myAppBarStyle == MyAppBarStyle.RED) {
      backgroundColor = MyColor.STYLE_RED;
      iconColor = Colors.white;
      brightness = Brightness.dark;
      textColor = Colors.white;
    } else if (myAppBarStyle == MyAppBarStyle.WHITE) {
      backgroundColor = Colors.white;
      iconColor = MyColor.TEXT_BLACK;
      brightness = Brightness.light;
      textColor = MyColor.TEXT_BLACK;
    }
  }
}

class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
  final dynamic title;
  final MyAppBarStyle myAppBarStyle;
  final List<Widget> actions;
  final Widget leading;
  const MyAppBar({Key key, this.title, this.myAppBarStyle = MyAppBarStyle.RED, this.actions, this.leading}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    _MyAppBarTheme myAppBarTheme = _MyAppBarTheme(myAppBarStyle);
    Widget titleWidget;
    if (title is String){
      titleWidget = Text(
        title,style: TextStyle(color: myAppBarTheme.textColor),
      );
    }
    else if(title is Widget){
      titleWidget = title;
    }
    else{
      titleWidget =Container();
    }
    return AppBar(
      leading: this.leading,
      titleSpacing: 5,
      title: titleWidget,
      backgroundColor: myAppBarTheme.backgroundColor,
      actionsIconTheme: IconThemeData(
        color: myAppBarTheme.iconColor,
      ),
      brightness: myAppBarTheme.brightness,
      actions: actions,
    );
  }
  @override
  // TODO: implement preferredSize
  Size get preferredSize => Size.fromHeight(kToolbarHeight);
}
